package com.atguigu.lease.web.admin.controller.apartment;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.LeaseTerm;
import com.atguigu.lease.web.admin.service.LeaseTermService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "租期管理")
@RequestMapping("/admin/term")
@RestController
public class LeaseTermController {

    @Autowired
    private LeaseTermService leaseTermService;

    /**
     * todo:逻辑：查询全部租期列表，单表查询
     * todo:实现步骤：
     *         1注入leaseTermService
     *         2.调用查询方法
     *         3.查询结果，装到result中
     *         4.返回result对象
     * @return
     */
    @GetMapping("list")
    @Operation(summary = "查询全部租期列表")
    public Result<List<LeaseTerm>> listLeaseTerm() {
        List<LeaseTerm> list = leaseTermService.list();
        Result<List<LeaseTerm>> result = Result.ok(list);
        return result;
    }
    /**
     * todo:逻辑：根据id是否存在，决定操作
     * 判定执行结果，并返回
     * @return
     */
    @PostMapping("saveOrUpdate")
    @Operation(summary = "保存或更新租期信息")
    public Result saveOrUpdate(@RequestBody LeaseTerm leaseTerm) {
        boolean is = leaseTermService.saveOrUpdate(leaseTerm);
        if(!is){
            return Result.fail();
        }
        return Result.ok();
    }

    /**
     * todo:逻辑：根据ID删除租期
     * @param id
     * @return
     */
    @DeleteMapping("deleteById")
    @Operation(summary = "根据ID删除租期")
    public Result deleteLeaseTermById(@RequestParam Long id) {
        leaseTermService.removeById(id);
        return Result.ok();
    }
}
